package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.HttpRequestImpl;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.ClientInfo;
import com.meichis.yndms.repository.ApiUtil;
import com.meichis.yndms.repository.RootApi;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class ClientNearListManager extends BaseManager {

    private static ClientNearListManager INSTANCE;
    private final RootApi rootAPI;

    public ClientNearListManager() {
        rootAPI = (RootApi) HttpRequestImpl.getInstance().getService(RootApi.class);
    }

    public static ClientNearListManager getInstance() {
        if (INSTANCE == null) {
            synchronized (ClientNearListManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new ClientNearListManager();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * @param showDialog
     * @param PartnerId   DMS经销商id
     * @param StaffId     DMS员工id
     * @param FindKey
     * @param Latitude
     * @param Longitude
     * @param MaxDistance 距离
     * @param PageIndex
     * @param PageSize    每页数量
     * @param data
     */
    public void GetNearRetailerList(final MutableLiveData<Boolean> showDialog, String PartnerId, String StaffId, String FindKey, double Latitude, double Longitude, int MaxDistance, int PageIndex, int PageSize, MutableLiveData<List<ClientInfo>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("PartnerId", PartnerId);
        hs.put("StaffId", StaffId);
        hs.put("FindKey", FindKey);
        hs.put("PageIndex", PageIndex);
        hs.put("PageSize", PageSize);
        hs.put("Latitude", Latitude);
        hs.put("Longitude", Longitude);
        hs.put("MaxDistance", MaxDistance);
        rootAPI.NearRetailerList(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<ClientInfo>>() {
                }.getType(), new IWebServiceCallback<List<ClientInfo>>() {

                    @Override
                    public void onSucceed(int sequence,List<ClientInfo> clientInfos, String ReturnInfo, int Return) {
                        if (clientInfos !=null&&clientInfos.size()>0){
                            data.setValue(clientInfos);
                        }else {
                            data.setValue(new ArrayList<>());
                        }

                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence,int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

}
